import { illegalArgumentsException } from "@/exception/illegalArgumentsException";
import { categoryApi, subjectApi, tagApi } from "@/request";
import type { bgwLabel } from "@/typedefined/Label";
import type { BgwCategory } from "@/typedefined/Category";
import { defineStore } from "pinia";
import { ref } from "vue";
import type { SubjectPage } from "@/typedefined/Page";
import type { bgwSubject } from "@/typedefined/Subject";

export const useBgwStores = defineStore("bgw",() => {
    // 一级标签列表
    const labelList = ref<Array<bgwLabel>>([]);

    // 分类列表
    const categoryPageList = ref<SubjectPage<BgwCategory>>();


    /**
     * 标签列表的setter
     * @param labelList_ 标签列表
     */
    function setLabelList(labelList_:Array<bgwLabel>){
        if(!labelList_) throw new illegalArgumentsException("labelList_参数不能为空");
        // 按照时间升序排列
        labelList.value = labelList_.sort((a, b) =>
            a.createdTime - b.createdTime
        );
    }

    /**
     * @description 获取标签列表
     */
    async function getLableList(){
        const dataList = await tagApi.getbgwTag() as unknown as Array<bgwLabel>;
        setLabelList(dataList);
    }

    /**
     * 分类列表的setter
     * @param categoryList_ 分类列表
     */
    function setCategoryList(categoryList_:SubjectPage<BgwCategory>){
        if(!categoryList_) throw new illegalArgumentsException("categoryList_参数不能为空");
        categoryPageList.value = categoryList_;
    }

    /**
     * @description 获取分类列表
     * @param currentPage 当前页码
     * @param pageSize 单页数据量
     */
    async function getCategoryList(currentPage:number,pageSize:number){
        // 获取标签列表
        const dataPageList = await categoryApi.getbgwCategory(currentPage,pageSize) as unknown as SubjectPage<BgwCategory>;
        setCategoryList(dataPageList);
    }

    /**
     * @description 添加题目
     * @param subject 题目实体
     */
    async function request2AddSubject(subject:bgwSubject){
        await subjectApi.bgw.addSubject(subject);
    }


    return {
        getLableList,getCategoryList,request2AddSubject,
        labelList,categoryPageList,
    }

});